from django.db import models
from sync.models import SFObject,SFField
from django.db.models import Q
from django.db import connection, transaction
from wizard.models.view import View
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.http import HttpResponseRedirect, HttpResponse

class MultiView(View):
    line_per_page=models.IntegerField(default=15)
    custome_sort=models.TextField(default="")
    custome_where=models.TextField(default="")
    class Meta:
        db_table = 'wizard_multi_view'
        app_label = 'wizard'
    """
    View using functions
    """
    def resp(self,request,dev=False):
        records=self.load(request)
        if self.custome_html:
            #TODO: add search form
            return render_to_response(self.custom_html, 
                              {'records':records},
                              context_instance = RequestContext(request))
        else:
            rendered=self.render(records)
            #TODO: add search form
            #TODO: add custom blocks
            return render_to_response('wizard/preview/multi.html', 
                              {'rendered':rendered},
                              context_instance = RequestContext(request))
    
    def load(self,request):
        records=self.obj.l_filter()[0:5]
        return records
    def get_where(self):
        pass
    def get_sort(self):
        pass
    def render(self,records):
        #get this view filds
        from wizard.models.view_field import ViewField
        fields=ViewField.objects.filter(view=self)
        html="<table><tr>"
        for f in fields:
            html+='<td>%s</td>'%f.label
        html+="</tr>"
        for r in records:
            html+="<tr>"
            for f in fields:
                print f.field.name
                html+='<td>'
                html+=r[f.field.name]
                html+='</td>'
                #jj=f.field
                #dddd=f.field.name
                #baba =bubu
            html+="</tr>"
        html+="</table>"
        #print html
        return html
